﻿#include <grid_manager.h>
#include "callback_funcs.h"
#include <mainwindow.h>
#include "ui_mainwindow.h"
#include <qdatetime.h>
#include <levent.h>
#include <QTimer>

Q_DECLARE_METATYPE(nlohmann::json)

void OnJoined(void* userData, ErrorCode eCode) {
    MainWindow* mainWnd = (MainWindow*)userData;
    //会议结束，恢复界面
    QVariant qq;
    qq.setValue(NULL);
    QPostMessage(mainWnd,666,qq);
}

void OnDisConnected(void* userData,int errCode) {

    MainWindow* mainWnd = (MainWindow*)userData;
    //会议结束，恢复界面
    QVariant qq;
    qq.setValue(NULL);
    QPostMessage(mainWnd,777,qq);
}

void OnNewMemberJoined(void* userData, const char* pMemberId, const char* pDisplayName,int cameras,int mics) {
    MainWindow* mainWnd = (MainWindow*)userData;
}

void OnModifyLocalAudioStatus(void* userData, bool bopen) {
    printf("OnModifyLocalAudioStatus  bSuccess = %d\n", bopen);
}


void OnMemberEnableVideo(void* userData, const char* pMemberId, const char* pDisplayName) {
    MainWindow* mainWnd = (MainWindow*)userData;
    if(std::string(pMemberId)!=mainWnd->_memberId){
      std::string peerId=pMemberId;
      QTimer::singleShot(0, mainWnd, [=]() {
        VideoWidget* video=mainWnd->_gridManager->getGridForPeer(peerId.c_str());
        video->setPeerId(peerId);
        video->setHasVideo(true);
        SetRenderWindow(mainWnd->m_handle, peerId.c_str(), (void*)video->winId(), true);
      });
    }
}

void OnMemberDisableVideo(void* userData, const char* pMemberId, const char* pDisplayName) {
    MainWindow* mainWnd = (MainWindow*)userData;
    if(std::string(pMemberId)!=mainWnd->_memberId){
        std::string strpMemberId=pMemberId;
        QTimer::singleShot(0, mainWnd, [=]() {
          mainWnd->_gridManager->ClosePeer(strpMemberId);
        });
    }
}

void OnMemberUnMuteAudio(void* userData, const char* pMemberId, const char* pDisplayName) {

    printf("OnMemberUnMuteAudio pMemberId=%s\n", pMemberId);
}

void OnMemberMuteAudio(void* userData, const char* pMemberId, const char* pDisplayName)
{
    printf("OnMemberMuteAudio pMemberId=%s\n", pMemberId);
}


void OnRecvUserData(void* userData, const char* data)
{
    printf("OnRecvUserData data=%s\n", data);
}

void OnSessionError(void* userData, const char* reason)
{
    printf("OnSessionError = %s\n", reason);
}

void OnGetUserDataHistory(void* userData, const char* data) {
    printf("OnGetUserDataHistory data=%s\n", data);
}

void OnMemberRoleChanged(void* userData, const char* pMemberId, const char* role) {

}
void GetAudioDevicesResult(void* userData, const DeviceInfo sDeviceInfos[], int nDeviceNum,bool in) {
    MainWindow* mainWnd = (MainWindow*)userData;
    if(in){
        mainWnd->ui->comboMics->clear();
        mainWnd->_micsMap.clear();
    }else{
        mainWnd->ui->comboSpeakers->clear();
        mainWnd->_speakersMap.clear();
    }
    for (int i = 0; i < nDeviceNum; i++) {
        QString audioName(sDeviceInfos[i].pDeviceName);
        if (in) {
            mainWnd->ui->comboMics->addItem(audioName);
            mainWnd->_micsMap.push_back(sDeviceInfos[i].nDeviceIndex);
        }
        else {
            mainWnd->ui->comboSpeakers->addItem(audioName);
            mainWnd->_speakersMap.push_back(sDeviceInfos[i].nDeviceIndex);
        }
    }
    if(in){
        //查找到对应的索引
        int index=-1;
        for(int i=0;i<mainWnd->_micsMap.size();i++){
            if(mainWnd->_suggestMic==mainWnd->_micsMap[i]){
                index=i;
                break;
            }
        }
        mainWnd->ui->comboMics->setCurrentIndex(index>=0?index:0);
    }
    else{
        int index=-1;
        for(int i=0;i<mainWnd->_speakersMap.size();i++){
            if(mainWnd->_suggestSpeaker==mainWnd->_speakersMap[i]){
                index=i;
                break;
            }
        }
        mainWnd->ui->comboSpeakers->setCurrentIndex(index>=0?index:0);
    }
}
void GetVideoDevicesResult(void* userData, const DeviceInfo sDeviceInfos[], int nDeviceNum) {
    MainWindow* mainWnd = (MainWindow*)userData;
    mainWnd->ui->comboCamera->clear();
    mainWnd->_camerasMap.clear();
    for (int i = 0; i < nDeviceNum; i++) {
        QString cameraName(sDeviceInfos[i].pDeviceName);
        mainWnd->ui->comboCamera->addItem(cameraName);
        mainWnd->_camerasMap.push_back(sDeviceInfos[i].nDeviceIndex);
    }
    mainWnd->ui->comboCamera->setCurrentIndex(0);
}
void OnDevicesChanged(void* userData,const char* pPeerId,int cameras,int mics,int speakers,int suggestMic,int suggestSpeaker){
    MainWindow* mainWnd = (MainWindow*)userData;
    //只处理本地的
    if(pPeerId)return;
    //获取摄像头列表
    if(mainWnd->_cameras != cameras){
        mainWnd->_cameras = cameras;
        GetVideoDevices(mainWnd->m_handle, GetVideoDevicesResult);
    }
    //获取麦克风列表
    if(mainWnd->_mics != mics){
        mainWnd->_mics = mics;
        mainWnd->_suggestMic=suggestMic;
        mainWnd->_suggestSpeaker=suggestSpeaker;
        GetAudioDevices(mainWnd->m_handle,true,GetAudioDevicesResult);
    }
    //获取扬声器列表
    if(mainWnd->_speakers != speakers){
        mainWnd->_speakers = speakers;
        mainWnd->_suggestMic=suggestMic;
        mainWnd->_suggestSpeaker=suggestSpeaker;
        GetAudioDevices(mainWnd->m_handle, false, GetAudioDevicesResult);
    }
}
void OnMemberEnableScreenShare(void* userData, const char* pMemberId, const char* pDisplayName) {
  MainWindow* mainWnd = (MainWindow*)userData;
  if (pMemberId != mainWnd->_memberId) {
    //显示屏幕共享窗口
    mainWnd->SetWidgetVisible(mainWnd->ui->screenWnd,true);
    SetScreenShareRenderWindow(
        mainWnd->m_handle, pMemberId,
        (void*)mainWnd->ui->screenWnd->winId(), false);
  }
}
void OnMemberDisableScreenShare(void* userData, const char* pMemberId, const char* pDisplayName) {
    MainWindow* mainWnd = (MainWindow*)userData;
    //隐藏屏幕共享窗口
    mainWnd->SetWidgetVisible(mainWnd->ui->screenWnd,false);
}

//空类型，离开会议
void OnPeerLeaved(void* userData,const char* peerId){
    MainWindow* mainWnd = (MainWindow*)userData;
    //离开会议，恢复界面
    mainWnd->ProcessPeerLeave(peerId);
}
void OnMeetingEnd(void* userData){
    MainWindow* mainWnd = (MainWindow*)userData;
    //会议结束，恢复界面
    QVariant qq;
    qq.setValue(NULL);
    QPostMessage(mainWnd,777,qq);
}
//视频链路回调,接收链路
void OnMediaLinkInfoFun(void* userData,const char* peerName,const char* lnkJson){
    //发送丢包率到界面数据做更新
    MainWindow* mainWnd = (MainWindow*)userData;
    nlohmann::json jRet=nlohmann::json::parse(lnkJson);
    QVariant msgData;
    msgData.setValue(jRet);
    QPostMessage(mainWnd,888,msgData);
}
//媒体状态信息
void OnMediaBandWidthInfoFun(void* userData,float upBandwith,float downBandwith){
    //发送数据到界面数据做更新
    MainWindow* mainWnd = (MainWindow*)userData;
    nlohmann::json jRet={
        {"upBandWidth",upBandwith},
        {"downBandWidth",downBandwith}
    };
    QVariant msgData;
    msgData.setValue(jRet);
    QPostMessage(mainWnd,890,msgData);
}
//音频链路回调
void OnPeerAudioVolumneFun(void* userData,const char* peerName,int volumn){
    MainWindow* mainWnd= (MainWindow*)userData;
    nlohmann::json jRet={
        {"peerName",std::string(peerName)},
        {"volumn",volumn}
    };
    //本地音量
    QVariant msgData;
    msgData.setValue(jRet);
    QPostMessage(mainWnd,880,msgData);
}

//屏幕共享回调
void OnEnableScreenshareCallback(void* ud,const char* jsonData){
}
